package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.YyRoom;
import com.ruoyi.system.mapper.YyAchievementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YyTeacherMapper;
import com.ruoyi.system.domain.YyTeacher;
import com.ruoyi.system.service.IYyTeacherService;
import com.ruoyi.common.core.text.Convert;

/**
 * 教师Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Service
public class YyTeacherServiceImpl implements IYyTeacherService
{
    @Autowired
    private YyTeacherMapper yyTeacherMapper;

    @Autowired
    private YyAchievementMapper yyAchievementMapper;


    @Autowired
    private SysUserServiceImpl sysUserService;

    /**
     * 查询教师
     *
     * @param id 教师主键
     * @return 教师
     */
    @Override
    public YyTeacher selectYyTeacherById(Long id)
    {
        return yyTeacherMapper.selectYyTeacherById(id);
    }

    @Override
    public List<YyTeacher> selectYyTeacherByIdList(Long id) {
        List<YyTeacher> YyTeacherByIdList = yyTeacherMapper.selectYyTeacherByIdList(id);
        List<YyTeacher> YyTeacherListAll = yyTeacherMapper.listAll();
        for (YyTeacher YyTeacherAll : YyTeacherListAll)
        {
            for (YyTeacher YyTeacherById : YyTeacherByIdList)
            {
                if (YyTeacherById.getId().longValue() == YyTeacherAll.getId().longValue())
                {
                    YyTeacherAll.setFlag(true);
                    break;
                }
            }
        }
        return YyTeacherListAll;
    }

    /**
     * 查询教师列表
     *
     * @param yyTeacher 教师
     * @return 教师
     */
    @Override
    public List<YyTeacher> selectYyTeacherList(YyTeacher yyTeacher)
    {
        //根据用户id获取角色
        Integer roleId = yyAchievementMapper.getRoleIdByUserId(yyTeacher.getUserId());
        if (roleId != null){
            yyTeacher.setRoleId(roleId);
        }
        return yyTeacherMapper.selectYyTeacherList(yyTeacher);
    }

    @Override
    public List<YyTeacher> listAll() {
        return yyTeacherMapper.listAll();
    }

    /**
     * 新增教师
     *
     * @param yyTeacher 教师
     * @return 结果
     */
    @Override
    public int insertYyTeacher(YyTeacher yyTeacher, SysUser sysUser)
    {
        //新增 系统用户
        sysUser.setStatus("0");
        Long[] array = {2L};
        sysUser.setRoleIds(array);
        sysUserService.insertUser(sysUser);
        yyTeacher.setUserId(sysUser.getUserId());
        return yyTeacherMapper.insertYyTeacher(yyTeacher);
    }

    /**
     * 修改教师
     *
     * @param yyTeacher 教师
     * @return 结果
     */
    @Override
    public int updateYyTeacher(YyTeacher yyTeacher)
    {
        return yyTeacherMapper.updateYyTeacher(yyTeacher);
    }

    /**
     * 批量删除教师
     *
     * @param ids 需要删除的教师主键
     * @return 结果
     */
    @Override
    public int deleteYyTeacherByIds(String ids)
    {
        return yyTeacherMapper.deleteYyTeacherByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除教师信息
     *
     * @param id 教师主键
     * @return 结果
     */
    @Override
    public int deleteYyTeacherById(Long id)
    {
        return yyTeacherMapper.deleteYyTeacherById(id);
    }
}
